package shiro.sso.chapter01.Reaml;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import org.apache.shiro.authc.*;
import org.apache.shiro.authz.AuthorizationInfo;
import org.apache.shiro.realm.AuthorizingRealm;
import org.apache.shiro.subject.PrincipalCollection;
import org.springframework.beans.factory.annotation.Autowired;
import shiro.sso.chapter01.entity.User;
import shiro.sso.chapter01.service.UserServiceImpl;

/**
 * Create by cheng on 2019/5/17 23:17
 */

public class MyShiroRealm extends AuthorizingRealm {

    @Autowired
    UserServiceImpl userService;

    @Override
    protected AuthorizationInfo doGetAuthorizationInfo(PrincipalCollection principalCollection) {
        return null;
    }

    @Override
    protected AuthenticationInfo doGetAuthenticationInfo(AuthenticationToken authenticationToken) throws AuthenticationException {
        String username = (String) authenticationToken.getPrincipal();
        QueryWrapper<User> qw = new QueryWrapper();
        qw.eq("username", username);
        User user = userService.getOne(qw);
        if (null == user) {
            throw new UnknownAccountException("该账户不存在");
        }
        AuthenticationInfo simpleAuthenticationInfo = new SimpleAuthenticationInfo(username, user.getPassword(), getName());
        System.out.println();
        return simpleAuthenticationInfo;
    }

}